package com.its.common.config;

import com.google.common.collect.Maps;
import com.its.common.util.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.SystemEnvironmentPropertySource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author xiaxp
 * @Date 3/9/22 15:12
 * @Description
 */
@Order(1000)
public class AutoConfigExcludeEnvSetting implements EnvironmentPostProcessor {
    private final static String ITS_DB_USE = "its.db.use";
    private final static String SPRING_AUTOCONFIG_EXCLUDE = "spring.autoconfigure.exclude";
    private final static String ITS_REDIS_ENABLED = "its.redis.enabled";
    private final static String TRUE = "true";
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        HashMap<String, Object> map = new HashMap<>();
        int startInx = getSpringAutoConfigCount(environment);
        startInx = handleDBBanExcludeSetting(environment, map, startInx);
        startInx = handleRedisBanExcludeSetting(environment, map, startInx);
    }

    private int handleDBBanExcludeSetting(ConfigurableEnvironment environment, HashMap<String, Object> map, int startInx){
        String dbUsed = environment.getProperty(ITS_DB_USE);
        if(StringUtils.isBlank(dbUsed)){
            map.put("its.db.use", TRUE);
        }else{
            System.err.println(">>>>>>>>>>>>>>>>>  本应用中不使用数据库  >>>>>>>>>>>>>>>>>");

            Map<String, Object> dynamicDatasourceEnabledMap = Maps.newHashMap();
            dynamicDatasourceEnabledMap.put("spring.datasource.dynamic","false");
            environment.getPropertySources().addLast(new SystemEnvironmentPropertySource(
                    "dynamicDatasourceEnabled",dynamicDatasourceEnabledMap
            ));

            map.put(SPRING_AUTOCONFIG_EXCLUDE + "[" + startInx++ +"]",
                    "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration");
            map.put(SPRING_AUTOCONFIG_EXCLUDE + "[" + startInx++ +"]",
                    "com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure");
            map.put(SPRING_AUTOCONFIG_EXCLUDE + "[" + startInx++ +"]",
                    "com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration");
        }

        PropertySource source = new MapPropertySource("DbUseEnvSettingExtend", map);
        environment.getPropertySources().addLast(source);
        return startInx;
    }
    private int handleRedisBanExcludeSetting(ConfigurableEnvironment environment, HashMap<String, Object> map, int startInx) {
        String redisEnable = environment.getProperty(ITS_REDIS_ENABLED);
        if (org.springframework.util.StringUtils.hasText(redisEnable) && redisEnable.contains(TRUE)) {
            return startInx;
        }
        map.put(SPRING_AUTOCONFIG_EXCLUDE + "[" + startInx++ + "]",
                "org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration");
        map.put(SPRING_AUTOCONFIG_EXCLUDE + "[" + startInx++ + "]",
                "org.redisson.spring.starter.RedissonAutoConfiguration");
        map.put(SPRING_AUTOCONFIG_EXCLUDE + "[" + startInx++ + "]",
                "com.its.common.redis.RedissonAutoConfigure");
//        map.put(SPRING_AUTOCONFIG_EXCLUDE + "[" + startInx++ +"]",
//                "org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration");
        map.put(SPRING_AUTOCONFIG_EXCLUDE + "[" + startInx++ + "]",
                "org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration");
        PropertySource source = new MapPropertySource("redisExtendExcludes", map);
        environment.getPropertySources().addLast(source);

        return startInx;
    }
    private int getSpringAutoConfigCount(ConfigurableEnvironment environment){
        int startInx = 0;
        for (PropertySource<?> source : environment.getPropertySources()) {
            if (source instanceof MapPropertySource) {
                MapPropertySource target = (MapPropertySource) source;
                for (String key : target.getSource().keySet()) {
                    if(key.contains(SPRING_AUTOCONFIG_EXCLUDE)){
                        startInx++;
                    }
                }
            }
        }
        return startInx;
    }
}
